<template>
	<div class="wrap" v-loading="loading" element-loading-text="等下,我正在请求" element-loading-spinner="el-icon-loading">
		<basic-opt :has_left="true">
			<template #search>
				<div class="opt-item">
					<el-input class="w-ipt" placeholder="请输入姓名进行筛选" suffix-icon="el-icon-search" v-model="search.name"
						@keydown.enter="getSearchData" size="medium">
					</el-input>
				</div>
				<div class="opt-item">
					<el-input class="w-ipt" placeholder="请输入手机号进行筛选" suffix-icon="el-icon-search" v-model="search.phone"
						@keydown.enter="getSearchData" size="medium">
					</el-input>
				</div>
				<div class="opt-item">
					<el-button @click="getSearchData" size="medium">搜索</el-button>
				</div>
				<div class="opt-item">
					<reset-search></reset-search>
				</div>
			</template>
			<template #add>
				<div class="opt-item">
					<el-button @click="addForm" icon="el-icon-circle-plus-outline" type="primary" size="medium">添加维护人员
					</el-button>
				</div>
			</template>
		</basic-opt>

		<div class="table-box">
			<el-table :data="data" stripe :header-cell-style="header_cell_style" :max-height="table_max_height">
				<el-table-column prop="id" label="ID" width="100px"></el-table-column>
				<el-table-column prop="name" label="名字"></el-table-column>
				<el-table-column prop="phone" label="联系方式"></el-table-column>
				<el-table-column prop="remark" label="备注"></el-table-column>
				<el-table-column label="操作" width="300">
					<template #default="scope">
						<div class="showOperation">
							<el-button @click="onEditData(scope.row)" type="success" plain size="mini">编辑</el-button>
							<el-button @click="onDelData(scope.row)" type="danger" plain size="mini">删除</el-button>
						</div>
					</template>
				</el-table-column>
			</el-table>
		</div>
		<page-component @pageChanged="onPageChanged"></page-component>

		<!-- 添加人脸 -->
		<compoment-drawer :drawer_name="drawer_name" :drawer_form="drawer_form" @getDrawerForm="getDrawerForm">
			<el-form ref="form" :model="form" label-width="120px">
				<el-scrollbar height="80vh" v-loading="form_loading" element-loading-text="正在进行请求,请稍后"
					element-loading-spinner="el-icon-loading">
					<div class="form-flex">
						<el-form-item label="姓名" prop="name">
							<el-input class="w-ipt" v-model="form.name" placeholder="请输入姓名"></el-input>
						</el-form-item>
						<el-form-item label="手机号" prop="phone">
							<el-input class="w-ipt" v-model="form.phone" placeholder="请输入手机号"></el-input>
						</el-form-item>
						<el-form-item label="备注" prop="remark">
							<el-input class="w-ipt" v-model="form.remark" placeholder="请输入备注"></el-input>
						</el-form-item>
					</div>
				</el-scrollbar>
				<submit-reset @getChildNewForm="getNewForm" @getChildResetForm="resetForm('form')">
				</submit-reset>
			</el-form>
		</compoment-drawer>

		<!-- 删除数据确认 -->
		<delete-data :del_data_dialog="del_data_dialog" @getDelData="getDelData" @getCancelDelData="getCancelDelData">
			<el-descriptions class="margin-top" :column="1" size="medium" border>
				<el-descriptions-item>
					<template #label>
						id
					</template>
					{{ delData.id }}
				</el-descriptions-item>
				<el-descriptions-item>
					<template #label>
						姓名
					</template>
					{{ delData.name }}
				</el-descriptions-item>
				<el-descriptions-item>
					<template #label>
						手机号
					</template>
					{{ delData.phone }}
				</el-descriptions-item>
				<el-descriptions-item>
					<template #label>
						备注
					</template>
					{{ delData.remark }}
				</el-descriptions-item>
			</el-descriptions>
		</delete-data>
	</div>

</template>

<script>
	import API from '@/api/index.js'
	import mixin from '@/mixin.js'

	export default {
		name: 'maintenance',
		mixins: [mixin],

		data() {
			return {
				search: {
					name: '',
					phone: ''
				},

				// 表单
				drawer_name: '添加维护人员',
				form_loading: false, // 表单填写时加载
				form: {
					name: '',
					phone: '',
					remark: ''
				},
			}
		},
		mounted() {
			this.getData();
		},
		methods: {
			async getData() {
				let self = this;
				self.loading = true;
				try {
					let params = {
						name: self.search.name ? self.search.name : '',
						phone: self.search.phone ? self.search.phone : ''
					}
					self.addPageInfo(params)
					let res = await API.maintenances(params.pageNumber, params.pageSize, params.name, params.phone)
					self.loading = false;
					self.onGotPageData(res)
				} catch (err) {
					self.loading = false;
				}
			},

			// 编辑数据
			onEditData(row) {
				let self = this;
				self.drawer_form = true;
				console.log(row)
				self.form = {
					id: row.id,
					name: row.name,
					phone: row.phone,
					remark: row.remark,
				}
			},
			// 确定删除数据
			submitDeleteData(val) {
				let self = this;
				API.delMaintenance(val).then(res => {
					console.log(res)
					self.getData();
					self.delSuccess();
				}).catch(err => {
					self.$message.error(err.response.data.message)
				})
			},
			// 提交表单
			getNewForm() {
				let self = this;
				self.form_loading = true;
				console.log(self.form)
				API.maintenance(self.form).then(res => {
					console.log(res)
					self.submitSuccess();
					self.drawer_form = false;
					self.form_loading = false;
					self.getData();
					self.initForm();
				}).catch(err => {
					self.form_loading = false;
					self.$message.error(err.response.data.message);
				});
			},

			// 初始表单
			initForm() {
				let self = this;
				self.form = {
					name: '',
					number: '',
					remark: ''
				}
			},

		}
	}
</script>
